﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Xml.Linq;

using CustomsSystem.BLL;
using CustomsSystem.Model;
using System.Collections.Generic;
using Maticsoft.Common;

namespace WEB.customer
{
    public partial class customerEdit : System.Web.UI.Page
    {
        CustomerManage cusManage = new CustomerManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCodeData(ddlCustomerType, "clientType");
                if (Request.QueryString["ID"] != null)   //修改
                {
                    #region 数据载入
                    int id = Convert.ToInt32(Request.QueryString["ID"]);
                    btnEdit.Text = "修改";
                    /*载入数据*/
                    Customer myCus = cusManage.GetModel(id);
                    tbAbb.Text = myCus.Abbreviation;
                    tbCusNo.Text = myCus.CustomerNO;
                    tbTaxNo.Text = myCus.TaxNo;
                    tbAddr.Text = myCus.Address;
                    tbCode.Text = myCus.PostCode;
                    tbCusName.Text = myCus.CustomerName;
                    tbInv.Text = myCus.Invoice;
                    ddlCustomerType.SelectedIndex = ddlCustomerType.Items.IndexOf(ddlCustomerType.Items.FindByValue(myCus.CustomerType));
                    tbMark.Text = myCus.Remarks;
                    ddlInvtype.SelectedIndex = ddlInvtype.Items.IndexOf(ddlInvtype.Items.FindByValue(myCus.InvType.ToString()));
                    Session["updateID"] = id;
                    #endregion
                }
                else //新增
                {
                    btnEdit.Text = "下一步";
                    Session["updateID"] = -1;
                }
            }
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            

           #region 有效性判断
            string strErr = "";
            #region 联系电话验证

            ////string Validation1 = tbTel.Text.Trim();
            //string validation2 = myCus.Fax;
            ////Regex MobilePhone = new Regex(@"^((?<国家代码>(\+86)|(\(\+86\)))?(?<手机号>((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1})|(14[0,5-9]{1})+\d{8})|(?<区号>(\d{3,4}-)|(\(\d{3,4}-))?\d{7,8}))$");
            //Regex MobilePhone = new Regex(@"^(?<国家代码>(\+86)|(\(\+86\)))?(?<手机号>((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\d{8})$");
            //Regex CPPhone = new Regex(@"(^(?<区号>(\d{3,4}-)|(\(\d{3,4}-))?\d{7,8})");
            #endregion

            #region 传真验证
            //string Validation2 = tbFax.Text.Trim();
            //Regex Fax = new Regex(@"(^(?<区号>(\d{3,4}-)|(\(\d{3,4}))\d{7,8})");
            #endregion

            if (this.tbCusName.Text.Trim().Length == 0)
            {
                strErr += "  客户名不能为空！\\n";
            }
            if (Session["updateID"].Equals(-1))
            {
                if (cusManage.GetList("CustomerName ='" + tbCusName.Text.Trim() + "'").Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show(this, "该公司已经存在，请查看客户信息！");
                    return;
                }
            }//客户信息验证
            if (tbMark.Text.Trim().Length >= 999)
            {
                strErr += "  工作要求栏的字数超过长度！\\n";
            }
            if (tbAddr.Text.Trim().Length >= 99)
            {
                strErr += "  地址栏的字数超过长度！\\n";
            }//字数验证
            if (strErr.Trim() != "")
            {
                strErr = "注意\\n" + strErr;
                MessageBox.Show(this, strErr);
                return;
            }
            if (ddlInvtype.SelectedValue == "-1")
            {
                MessageBox.Show(this, "请选择发票类型！");
                return;
            }
            if (ddlCustomerType.SelectedValue == "-1")
            {
                MessageBox.Show(this, "请选择顾客类型！");
                return;
            }
            #endregion
            #region 数据录入
            Customer myCus = new Customer();
            myCus.ID = Convert.ToInt32(Session["updateID"]);
            myCus.Abbreviation = tbAbb.Text.Trim();
            myCus.CustomerNO = tbCusNo.Text.Trim();
            myCus.TaxNo = tbTaxNo.Text.Trim();
            //读取下拉框的值
            myCus.InvType = int.Parse(ddlInvtype.SelectedValue);
            myCus.Address = tbAddr.Text.Trim();
            myCus.PostCode = tbCode.Text.Trim();
            myCus.CustomerName = tbCusName.Text.Trim();
            myCus.Invoice = tbInv.Text.Trim();
            myCus.CustomerType = ddlCustomerType.Text.Trim();
            myCus.Remarks = tbMark.Text.Trim();

            if (myCus.ID == -1)//增加记录
            {
                int id = cusManage.Add(myCus);
                if (id > 0)
                {
                    MessageBox.ShowAndRedirect(this, "增加客户成功，进行联系人的添加", "/contact/contactEdit.aspx?AddID=" + id);
                }//增加成功
                else
                {
                    Response.Write("<script>alert('增加失败!')</script>");//可能是因为已存在该客户!
                }
            }
            else    //修改记录
            {
                if (cusManage.Update(myCus))
                {
                    Response.Write("<script>alert('修改成功!')</script>");
                    Response.Write("<script>window.document.location.href='/customer/customerView.aspx'</script>");
                }
                else
                {
                    Response.Write("<script>alert('修改失败!')</script>");
                }
            }
            #endregion
        }

        /// <summary>
        /// 获取制定代码类型绑定到控件上
        /// </summary>
        /// <param name="ddl"></param>
        private void BindCodeData(DropDownList ddl, string codeType)
        {
            CodeManage codeBll = new CodeManage();
            List<Info_code> codeModels = codeBll.GetModelList("codeType='" + codeType + "'");
            foreach (Info_code aCode in codeModels)
            {
                ddl.Items.Add(new ListItem(aCode.codeName, aCode.codeID.ToString()));
            }
        }

        protected void btnBank_Click(object sender, EventArgs e)
        {
            Session["CustomerName"] = tbCusName.Text.Trim();
            Response.Redirect("~/Bank/BankAdd.aspx");
        }
    }
}